* The analysis was conducted using Stata 16. 
clear
* Change directory to where the folder with the data is
cd "Data Archive TWT Predictions\KTHDP"

use DatasetKTHDP.dta,clear

*check the number of subjects
tab subj

*check number of pairs
tab idpair

*sort the dataset by the subjects' identifier, the identifier of the choice pair, and response times
sort subj idpair rt

*create the panel structure of the dataset in order to create choice proportions and calculate the number of repetitions
egen panelid = group(subj idpair)
bysort subj idpair : gen Counting=_n
egen maxrep = max(Counting), by(subj idpair)
xtset panelid Counting

*create choice proportion variables
egen sumleft=sum(choiceleft), by(subj idpair)
gen sumright = repetition -sumleft
gen Ratioleft = sumleft/repetition
gen Ratioright = sumright/repetition

*trasform response times and get response times conditiona of choices
gen logrt=log(rt)
gen logrtA = log(rt) if choiceleft==1
gen logrtB = log(rt) if choiceleft==0

*preferences are always revealed for deterministic choices
gen deterministic=0
replace deterministic =1 if sumleft==maxrep | sumleft==0
tab det

*sort the dataset based on all response times, this gives the grid of points for which the CDF of RTs will be calculated.
sort rt, stable
save baseForDensities, replace

use baseForDensities, clear
*Calculate the CDF of RTs by looping over subjects and choice pairs. Use a non-adaptive kernel with epanechnikov specification and bandwidth of 0.01 as in Alos-Ferrer, Fehr, and Netzer (2021).
*Because of how akdensity is implemented, save the estimated CDFs conditional on the choice (left vs. right) separately and then merge them back to the dataset.	

local j = 1
          while `j' < 31{
 local i = 1
          while `i' < 46{
          gen rtAs`j'id`i' = logrtA if (sumleft>1 & idpair==`i'  & subject==`j')
    sum rtAs`j'id`i'
    if r(N)!=0{
    di as result "idpair `i'" as error "subject`j'"
akdensity rtAs`j'id`i'   , nograph  noadaptive   at(logrt) cdf(cdfAsubj`j'id`i') 
          }
		  drop rtAs`j'id`i'
local i = `i' + 1
            }   
local j = `j' + 1
            }	
			
*Density estimation with only one point		
 local j = 1
          while `j' < 31{
 local i = 1
          while `i' < 46{
          gen rtAs`j'id`i' = logrtA if (sumleft==1  & idpair==`i'  & subject==`j')
    sum rtAs`j'id`i'
    if r(N)!=0{
    di as result "idpair `i'" as error "subject`j'"
akdensity rtAs`j'id`i'   , nograph kernel(epanechnikov) noadaptive bwidth(0.01)  at(logrt) cdf(cdfAsubj`j'id`i') 
          }
		  drop rtAs`j'id`i'
local i = `i' + 1
            }    
local j = `j' + 1
            }					
 save LeftChoice, replace

use baseForDensities, clear
 local j = 1
          while `j' < 31{
 local i = 1
          while `i' < 46{
          gen rtBs`j'id`i' = logrtB if (sumright>1 & idpair==`i'  & subject==`j')
    sum rtBs`j'id`i'
    if r(N)!=0{
    di as result "idpair `i'" as error "subject`j'"
akdensity rtBs`j'id`i'   , nograph kernel(epanechnikov) noadaptive   at(logrt) cdf(cdfBsubj`j'id`i') 
          }
		  drop rtBs`j'id`i'
local i = `i' + 1
            }   
local j = `j' + 1
            }	
			
*Density estimation with only one point							
 local j = 1
          while `j' < 31{
 local i = 1
          while `i' < 46{
          gen rtBs`j'id`i' = logrtB if (sumright==1  & idpair==`i'  & subject==`j')
    sum rtBs`j'id`i'
    if r(N)!=0{
    di as result "idpair `i'" as error "subject`j'"
akdensity rtBs`j'id`i'   , nograph kernel(epanechnikov) noadaptive bwidth(0.01)  at(logrt) cdf(cdfBsubj`j'id`i') 
          }
		  drop rtBs`j'id`i'
local i = `i' + 1
            }    
local j = `j' + 1
            }					
 save RightChoice, replace

*Merge the estimated CDFs conditional on choices back
use baseForDensities,clear 
sort rt, stable
merge 1:1 subj trial using LeftChoice, nogenerate
merge 1:1 subj trial using RightChoice, nogenerate

*Check if the conditions identified by Theorem 1 (Alos-Ferrer, Fehr, and Netzer, 2021, Theorem 2) are fulfilled.
*Loop over subjects and pairs
gen revpref=.
quietly{
local j = 1
          while `j' < 31{
local i = 1
         while `i' < 46{
	capture confirm variable cdfAsubj`j'id`i'
        if !_rc {
	capture confirm variable cdfBsubj`j'id`i'
        if !_rc {		
	 gen ratioAsubj`j'id`i' = Ratioleft  if  idpair==`i' & subj==`j'
	 gen ratioBsubj`j'id`i' = Ratioright if  idpair==`i' & subj==`j' 
gen pfpref  = ratioAsubj`j'id`i'  * cdfAsubj`j'id`i'  
gen pfnotpref  = ratioBsubj`j'id`i'  * cdfBsubj`j'id`i' 
gen diff  =  pfpref - pfnotpref 
sum diff, detail
egen qfsdh1=max(sign(diff))
egen qfsdh2=min(sign(diff))
gen qfsd`j'`i'=qfsdh1-qfsdh2
replace qfsd`j'`i'=1 if qfsd`j'`i'==0
replace qfsd`j'`i'=0 if qfsd`j'`i'==2

*Default is no preference revelation
gen revpref`j'id`i'=0 if qfsd`j'`i'==0

*Q-FSD is revealed
replace revpref`j'id`i'=1 if qfsd`j'`i'==1
replace revpref =revpref`j'id`i'  if  idpair==`i' & subj==`j'
drop ratioAsubj`j'id`i' ratioBsubj`j'id`i' qfsd`j'`i' revpref`j'id`i' diff pfpref pfnotpref qfsdh1  qfsdh2
}
}
local i = `i' + 1
            }
local j = `j' + 1
            }
}

tab revpref
*deterministic choices are always revealed			
replace revpref =1  if Ratioleft==1 & Ratioright==0
replace revpref =1  if Ratioleft==0 & Ratioright==1
tab revpref, missing

egen meanrevpref= mean(revpref), by(subj)
sum meanrevpref
save revealedprefKTHDP, replace
